<?php

namespace App\Models;

use App\MongoBaseModel as Model;
use App\Plugins\Wechat\Client;
use Helper;
use MongoDB\BSON\Regex;

/**
 * Class WxClient
 * @package App\Models
 * @property string id
 * @property string mobile      微信手机号
 * @property string password    微信密码
 * @property string nickName    微信号昵称
 * @property string status     手机号存放位
 * @property string userName    微信分配给用户的@UserName
 * @property int    uin         微信分配给用户的唯一性ID
 * @property string uuid        此微信号最新使用的uuid
 * @property string qrcode      此微信号加好友的二维码图片, Base64格式
 * @property string headImg     此微信使用的头像地址
 * @property string loginMobile 此微信登录是用的手机
 * @property string project_id  保存分配给微信号的项目ID, 没有project_id或者project_id为空的微信号为未使用。
 * @property string company_id  保存分配给微信号的公司ID。
 * @property \stdClass $clientInfo  微信客户端侧的信息
 * {
 *     userName: '',
 *     nickName: '',
 *     remarkName: '',
 *     city: '',
 *     uin: '',
 *     pYQuanPin: '',
 *     remarkPYInitial: '',
 * }
 * @property array contacts     微信好友
 * [
 *   {
 *     userName:'',        # 好友在微信中的唯一标识
 *     nickName:'',        # 好友的微信昵称
 *     customer_id: '',    # 好友在系统中的ID
 *     addTime: '',        # 好友添加的时间
 *   },
 *
 * ]
 * @property array groups
 */
class WxClient extends Model
{

    protected $fillable = ['mobile', 'password', 'nickName', 'loginMobile', 'status', 'qrcode', 'uin'];
    protected $casts = [
        'clientInfo' => 'object',
    ];
    protected $appends = ['contactNum'];
    const WXCLIENT_LIST_FIELDS = [
        'mobile',
        'index',
        'nickName',
        'password',
        'uin',
        'companyName',
        'projectName',
        'contactName',
        'contacts',
        'loginStatus',
        'loginMobile',
        'status',
        'operation',
    ];
    const WXClIENT_SHOW_FIELDS = [
        'index',
        'nickName',
        'password',
        'loginMobile',
        'status',
        'qrcode',
    ];
    const WXClIENT_SET_FIELDS = ['nickName', 'mobile', 'index', 'project_id', 'company_id'];
    const WXClIENT_OPT_FIELDS = ['loginStatus', 'status', 'operation'];

    //好友数
    public function getContactNumAttribute() {
        if ($this->contacts) {
            return count($this->contacts);
        }
        return 0;
    }

    public function scopeSearch($query, $inputs){
        if(!empty($inputs)){
            return $query->where(function ($querys) use ($inputs){
                if(!empty($inputs['baseInfo'])){
                    if (is_numeric($inputs['baseInfo'])) {
                        $querys->where([
                            '$or' => [
                                ['mobile' => ['$regex' => (string) $inputs['baseInfo']]],
                                ['uin' => (int) $inputs['baseInfo']],
                                ['nickName' => ['$regex' => (string) $inputs['baseInfo']]],
                            ]]);
                    } else {
                        $querys->where([
                                'nickName' => ['$regex' => (string) $inputs['baseInfo']],
                            ]);
                    }
                }
                if(!empty($inputs['project_id'])) {
                    $querys->where('project_id', $inputs['project_id']);
                }
                if (isset($inputs['status'])
                    && in_array($inputs['status'], [0,1,2])) {
                    $querys->where('status', $inputs['status']);
                }
            });
        }
    }

    public function scopeUsed($query)
    {
        return $query
            ->where([
                'project_id' => new Regex('\w+', 'i')
            ]);
    }

    public function scopeInCompany($query, $company_id)
    {
        return $query->where(['company_id' => $company_id]);
    }

    public function company() {
        return $this->belongsTo(Company::class);
    }

    public function project() {
        return $this->belongsTo(Project::class);
    }

    public function simCard() {
        return $this->belongsTo(SimCard::class, 'mobile', 'mobile');
    }
}
